define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function ($, undefined, Backend, Table, Form, Template) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init();

            //绑定事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var panel = $($(this).attr("href"));
                if (panel.size() > 0) {
                    Controller.table[panel.attr("id")].call(this);
                    $(this).on('click', function (e) {
                        $($(this).attr("href")).find(".btn-refresh").trigger("click");
                    });
                }
                //移除绑定的事件
                $(this).unbind('shown.bs.tab');
            });

            //必须默认触发shown.bs.tab事件
            $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");
        },
        table: {
            dorm: function () {
                var table_dorm = $("#table_dorm");
                table_dorm.bootstrapTable({
                    url: 'record/words/data?type=dorm',
                    extend: {
                        index_url: 'record/words/data?type=dorm',
                        add_url: '',
                        edit_url: '',
                        del_url: 'record/words/del',
                        multi_url: '',
                        table: '',
                    },
                    toolbar: '#toolbar_dorm',
                    sortName: 'id',
                    search: false,
                    columns: [
                        [
                            {field: 'state', checkbox: true, },
                            {field: 'id', title: 'ID', visible: false, operate: false,},
                            {field: 'json_data.school_name', title: '学校', operate: false,},
                            {field: 'json_data.garden_name', title: '园区', operate: false,},
                            {field: 'json_data.building_name', title: '楼栋', operate: false,},
                            {field: 'dorm_floor', title: '所在楼层', operate: false,},
                            {field: 'json_data.dorm_number', title: '寝室编号', operate: false,},
                            {field: 'dorm_used_count', title: '寝室人数', operate: false,},
                            {field: 'comment', title: '文字内容', operate: false,},
                            {field: 'image_count', title: '图片个数', operate: false,},
                            {
                                field: 'operate',
                                title: __('Operate'),
                                table: table_dorm,
                                events: Table.api.events.operate,
                                buttons: [
                                    {
                                        name: '查看',
                                        title: '查看',
                                        text: '查看',
                                        icon: 'fa fa-check-circle-o',
                                        classname: 'btn btn-success btn-xs btn-click',
                                        click: function (e, data) {
                                            Backend.api.open('record/words/detail?type=dorm&ids=' + data.id, __('查看'), {area: ['70%', '80%']});
                                        }
                                    }
                                ],
                                formatter: Table.api.formatter.operate
                            },

                        ]
                    ]
                });

                Table.api.bindevent(table_dorm);
            },
            classes: function () {
                var table_classes = $("#table_classes");
                table_classes.bootstrapTable({
                    url: 'record/words/data?type=classes',
                    extend: {
                        index_url: 'record/words/data?type=classes',
                        add_url: '',
                        edit_url: '',
                        del_url: 'record/words/del',
                        multi_url: '',
                        table: '',
                    },
                    toolbar: '#toolbar_classes',
                    sortName: 'id',
                    search: false,
                    columns: [
                        [
                            {field: 'state', checkbox: true, },
                            {field: 'id', title: 'ID', visible: false, operate: false,},
                            {field: 'school_name', title: '学校', operate: false,},
                            {field: 'grade_name', title: '年级', operate: false,},
                            {field: 'class_name', title: '班级', operate: false,},
                            {field: 'headmaster_name', title: '班主任', operate: false,},
                            {field: 'comment', title: '文字内容', operate: false,},
                            {field: 'image_count', title: '图片个数', operate: false,},
                            {
                                field: 'operate',
                                title: __('Operate'),
                                table: table_classes,
                                events: Table.api.events.operate,
                                buttons: [
                                    {
                                        name: '查看',
                                        title: '班级查看',
                                        text: '查看',
                                        icon: 'fa fa-check-circle-o',
                                        classname: 'btn btn-success btn-xs btn-click',
                                        click: function (e, data) {
                                            Backend.api.open('record/words/detail?type=classes&ids=' + data.id, __('班级查看'), {area: ['70%', '80%']});
                                        }
                                    }
                                ],
                                formatter: Table.api.formatter.operate
                            },

                        ]
                    ]
                });

                Table.api.bindevent(table_classes);
            },
            student: function () {
                var table_student = $("#table_student");
                table_student.bootstrapTable({
                    url: 'record/words/data?type=student',
                    extend: {
                        index_url: 'record/words/data?type=student',
                        add_url: '',
                        edit_url: '',
                        del_url: 'record/words/del',
                        multi_url: '',
                        table: '',
                    },
                    toolbar: '#toolbar_student',
                    sortName: 'id',
                    search: false,
                    columns: [
                        [
                            {field: 'state', checkbox: true, },
                            {field: 'id', title: 'ID', visible: false, operate: false},
                            {field: 'school_name', title: '学校', operate: false,},
                            {field: 'name', title: '姓名', operate: false,},
                            {field: 'gender_text', title: '性别', operate: false,},
                            {field: 'comment', title: '文字内容', operate: false,},
                            {field: 'image_count', title: '图片个数', operate: false,},
                            {
                                field: 'operate',
                                title: __('Operate'),
                                table: table_student,
                                events: Table.api.events.operate,
                                buttons: [
                                    {
                                        name: '查看',
                                        title: '查看',
                                        text: '查看',
                                        icon: 'fa fa-check-circle-o',
                                        classname: 'btn btn-success btn-xs btn-click',
                                        click: function (e, data) {
                                            Backend.api.open('record/words/detail?type=student&ids=' + data.id, __('查看'), {area: ['70%', '80%']});
                                        }
                                    }
                                ],
                                formatter: Table.api.formatter.operate
                            },
                        ]
                    ]
                });

                Table.api.bindevent(table_student);
            }
        },
        detail: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: '',
                    detail_url: 'record/words/detail?ids=' + Config.admin.ids + '&type=' + Config.admin.type,
                    add_url: '',
                    edit_url: '',
                    del_url: 'album/del',
                    multi_url: '',
                }
            });

            var table = $("#table");

            Template.helper("Moment", Moment);

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.detail_url,
                templateView: true,
                columns: [
                    [
                        {field: 'state', checkbox: true,},
                        {field: 'url', title: __('图片'), operate: false},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ],
                ],
                //禁用默认搜索
                search: false,
                //启用普通表单搜索
                commonSearch: false,
                //可以控制是否默认显示搜索单表,false则隐藏,默认为false
                searchFormVisible: false,
                //分页大小
                pageSize: 12,
                showToggle: false,
                showColumns: false,
                showExport: false,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            $(".toolbar > .btn-del,.toolbar .btn-more~ul>li>a").data("success", function (e) {
                Controller.api.refreshtable(Config.admin.type);
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            refreshtable: function (type) {
                $('#table_' + type).bootstrapTable('refresh');
                window.parent.$(".toolbar .btn-refresh").trigger('click');
            },
        }
    };
    return Controller;
});